﻿using log4net;
using log4net.Config;
using System;
using System.IO;

namespace Infrastructure.Log
{
    /// <summary>
    /// 基于LOG4NT的日志
    /// </summary>
    public class Logger
    {

        static Logger()
        {
            XmlConfigurator.Configure(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CfgFiles\\log4net.cfg.xml")));
            //  ILog log = LogManager.GetLogger(typeof(Logger));
        }

        private readonly ILog _loger;

        /// <summary>
        /// log4日志
        /// </summary>
        /// <param name="type"></param>
        public Logger(Type type)
        {
            _loger = LogManager.GetLogger(type);
        }

        /// <summary>
        /// 从根目录重读配置文件
        /// </summary>
        /// <param name="filePath"></param>
        public void ReloadConfigFromBaseDirectory(string filePath = "log4net.cfg.xml")
        {
            XmlConfigurator.Configure(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filePath)));
        }


        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ex"></param>
        public void Fatal(string msg = "出现异常", Exception ex = null)
        {
            Console.WriteLine(msg);
            _loger.Fatal(msg, ex);
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="ex"></param>
        public void Error(string msg = "出现异常", Exception ex = null)
        {
            Console.WriteLine(msg);
            _loger.Error(msg, ex);
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        public void Warn(string msg)
        {
            Console.WriteLine(msg);
            _loger.Warn(msg);
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        public void Info(string msg)
        {
            Console.WriteLine(msg);
            _loger.Info(msg);
        }

        /// <summary>
        /// Log4日志
        /// </summary>
        /// <param name="msg"></param>
        public void Debug(string msg)
        {
            Console.WriteLine(msg);
            _loger.Debug(msg);
        }
    }
}
